package com.wdg.common.factoy;

import com.wdg.common.annotation.NeedAuth;
import com.wdg.common.basemodule.BusinessException;
import com.wdg.common.userInfo.CurrentUserInfo;
import com.wdg.common.enums.ResponseEnum;
import com.wdg.common.userInfo.UserInfo;
import com.wdg.utils.Base64Util;
import com.wdg.utils.JWTUtils;
import com.wdg.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.Objects;

//@Component
@Slf4j
public class WoWoAIHandlerInterceptor implements HandlerInterceptor {

    private static String USER_INFO_KEY = "wi_userInfo";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //判断当前接口需要登录
//        1.如果需要登录则判断用户的权限是否有
        // 将handler强转为HandlerMethod, 前面已经证实这个handler就是HandlerMethod
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        // 从方法处理器中获取出要调用的方法
        Method method = handlerMethod.getMethod();
        // 获取出方法上的Access注解
        NeedAuth needAuth = method.getAnnotation(NeedAuth.class);
        if (Objects.isNull(needAuth)) {
            //不需要登录直接放开
            return Boolean.TRUE;
        }
        //todo 这里以后可能需要判断角色
        this.checkRule(needAuth);
        this.checkToken(request, response);
        String userInfoJSon = request.getHeader(USER_INFO_KEY);
        log.debug("获取的用户信息 = {}", JsonUtils.toJsonString(userInfoJSon));
        if (StringUtils.isNotEmpty(userInfoJSon)){
            UserInfo userInfoDecrypt = Base64Util.decrypt(userInfoJSon);
            CurrentUserInfo.setUserInfo(userInfoDecrypt);
        }
        return Boolean.TRUE;
    }

    /**
     * 校验角色权限
     * @param needAuth
     */
    private void checkRule(NeedAuth needAuth) {
    }

    private void checkToken(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        // 解析token
        String authorization = request.getHeader(JWTUtils.header);
        String token = JWTUtils.validateToken(authorization);
        if (StringUtils.isEmpty(token)){
            throw new BusinessException(ResponseEnum.NO_AUTH.getCode(),"没有权限");
        }
        if (JWTUtils.isNeedUpdate(authorization)){
            String newToken = JWTUtils.createToken(token);
            response.setHeader(JWTUtils.USER_LOGIN_TOKEN,newToken);
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        CurrentUserInfo.removeUserInfo();
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
